package com.zml.solarcontrol;

import com.huawei.ailifeability.NetConfigAbility;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.ace.ability.AceAbility;
import ohos.utils.zson.ZSONObject;

import java.util.Base64;
import java.util.Objects;

/**
 * This ability is pulled up after OneHop. Session id and device sn are delivered by intent. sessionId is used to
 * config the device network, deviceSn is the device specified in the NFC tag.
 * If you want to config the device network simply, use the default JS module.
 * If you want to use huawei account, use the login JS module. Modify {@link MainAbility#JS_MODULE}
 * The default productId is FAN, we also provider LAMP、TOOTHBRUSH、SOYMILKMACHINE.
 */
public class MainAbility extends AceAbility {
    private static final String DEFAULT_MODULE = "default";
    private static final String LOGIN_MODULE = "login";
    private static final String JS_MODULE = DEFAULT_MODULE;
    private static String productId;
//    private String productName = "FAN";
    private String productName = "SOLAR";

    @Override
    public void onStart(Intent intent) {
        intent.setParam("window_modal", 3);
        setInstanceName(JS_MODULE);

        Object productInfo = Objects.requireNonNull(intent.getParams()).getParam("productInfo");    // 提取NFC标签内容
        if (productInfo != null) {
            productId = (String) productInfo;
            System.out.println("++++++++++++++++++++ productId:"+productId);
            // modify your product name according to your product id in nfc tag.
            productName = "SOLAR";
        }

        Object businessInfo = Objects.requireNonNull(intent.getParams()).getParam("businessInfo");
        if (businessInfo != null) {
            ZSONObject businessInfoZSON = ZSONObject.classToZSON(businessInfo);
            // data91 is your device sn in nfc tag.
            String data91 = businessInfoZSON.getZSONObject("params").getString("91");
            // device sn is used to check your device validity
            String deviceSn = base64Decoder(data91);
            System.out.println("++++++++++++++++++++ deviceSn:"+deviceSn);
            System.out.println("++++++++++++++++++++ data91:"+data91);
        }
        String sessionId = intent.getStringParam("nanSessionId");
        if ("null".equals(sessionId) || "".equals(sessionId)) {
            NetConfigAbility.register(this, null);
        } else {
            NetConfigAbility.register(this, sessionId);
        }

        IntentParams intentParams = intent.getParams();
        if (intentParams != null) {
            intentParams.setParam("productId", productId);
            intentParams.setParam("productName", productName);
            intentParams.setParam("sessionId", sessionId);
            System.out.println("++++++++++++++++++++ productId:"+productId);
            System.out.println("++++++++++++++++++++ productName:"+productName);
            System.out.println("++++++++++++++++++++ sessionId:"+sessionId);
            setPageParams(null, intentParams);
        }
        super.onStart(intent);
    }

    @Override
    public void onStop() {
        super.onStop();
    }


    private String base64Decoder(String data91) {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(data91);
        StringBuilder stringBuilder = new StringBuilder();
        for (byte byteData : bytes) {
            stringBuilder.append((char) byteData);
        }
        return stringBuilder.toString();
    }
}
